<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;


/**
 * App\Models\Material
 *
 * @property int $id
 * @property int $uid
 * @property string $username
 * @property int $albumid 专辑ID，图片素材有效
 * @property string $name
 * @property string $source
 * @property string $thumb
 * @property string $width
 * @property string $height
 * @property string $type
 * @property string $extension 扩展名
 * @property int $size
 * @property int $views
 * @property int $downloads
 * @property string $created_at
 * @property string $updated_at
 * @property-read \App\Models\User $user
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Material doc()
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Material file()
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Material image()
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Material video()
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Material voice()
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Material whereAlbumid($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Material whereCreatedAt($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Material whereDownloads($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Material whereExtension($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Material whereHeight($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Material whereId($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Material whereName($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Material whereSize($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Material whereSource($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Material whereThumb($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Material whereType($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Material whereUid($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Material whereUpdatedAt($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Material whereUsername($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Material whereViews($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Material whereWidth($value)
 * @mixin \Eloquent
 */
class Material extends Model
{
    protected $table = 'material';
    protected $primaryKey = 'id';
    protected $guarded = ['id'];

    public $timestamps = false;

    public static function boot()
    {
        parent::boot(); // TODO: Change the autogenerated stub
        static::creating(function ($model){
            $model->created_at = time();
            $model->updated_at = time();
        });

        static::updating(function ($model){
            $model->updated_at = time();
        });
    }

    /**
     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
     */
    public function user()
    {
        return $this->belongsTo(User::class, 'uid', 'uid');
    }

    /**
     * @param $query
     * @return mixed
     */
    public function scopeImage($query)
    {
        return $query->where('type', 'image');
    }

    /**
     * @param $query
     * @return mixed
     */
    public function scopeVideo($query)
    {
        return $query->where('type', 'video');
    }

    /**
     * @param $query
     * @return mixed
     */
    public function scopeVoice($query)
    {
        return $query->where('type', 'voice');
    }

    /**
     * @param $query
     * @return mixed
     */
    public function scopeDoc($query)
    {
        return $query->where('type', 'doc');
    }

    /**
     * @param $query
     * @return mixed
     */
    public function scopeFile($query)
    {
        return $query->where('type', 'file');
    }
}
